<template>
  <page>
    <view class="input-info">
      <view class="input-text" v-if="type === '0'">
        <view class="content">
          <input type="text" v-model="nickname" placeholder="请输入你的昵称">
        </view>
        <view class="hint">
          <text>好名字可以让你的朋友更容易记住你。</text>
        </view>
      </view>
      <view class="input-text" v-else-if="type === '1'">
        <view class="content">
          <input type="text" v-model="signature" placeholder="请输入你的签名">
        </view>
        <view class="hint">
          <text>好的签名，更彰显你的魅力。</text>
        </view>
      </view>
    </view>
    <view class="save-btn">
      <view class="btn" @click="onSave">
        <text>保存</text>
      </view>
    </view>
  </page>
</template>

<script setup>
import {getCurrentInstance, ref} from "vue";
import {editNickname, editSignature} from "../../../api/api";
import {onLoad} from "@dcloudio/uni-app";

const { proxy } = getCurrentInstance();

const type = ref('');
const nickname = ref('');
const signature = ref('');
const info = ref({});

onLoad((params) =>{
  type.value = params.type||'0';
  const eventChannel = proxy.getOpenerEventChannel();
  eventChannel.on('userInfo', function(data) {
    info.value = data.data;
    nickname.value = data.data.nickname;
    signature.value = data.data.signature;
  })
})

/**
 * 保存点击
 */
function onSave(){
  if(!nickname.value){
    proxy.$way.toast('昵称不能为空');
    return;
  }
  if(type.value === '0'){
    editNickname({
      nickname: nickname.value,
    }).then(res =>{
      proxy.$way.toast(res.msg);
      uni.navigateBack();
    })
  }else if(type.value === '1'){
    editSignature({
      signature: signature.value,
    }).then(res =>{
      proxy.$way.toast(res.msg);
      uni.navigateBack();
    })
  }
}
</script>

<style lang="scss">
@import "EditInfo.scss";
</style>
